package com.bougaga.opensource.scheduler.web.controller;

import com.bougaga.opensource.scheduler.core.scheduler.DelayedTaskScheduler;
import com.bougaga.opensource.scheduler.core.scheduler.SchedulerStatistics;
import com.bougaga.opensource.scheduler.core.task.TaskStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Web页面控制器
 * 
 * @author bougaga
 * @since 1.0.0
 */
@Controller
public class WebController {
    
    private static final Logger logger = LoggerFactory.getLogger(WebController.class);
    
    private final DelayedTaskScheduler scheduler;
    
    @Autowired
    public WebController(DelayedTaskScheduler scheduler) {
        this.scheduler = scheduler;
        logger.info("WebController initialized with scheduler: {}", scheduler);
    }
    
    /**
     * 主页面
     */
    @GetMapping("/")
    public String index(Model model) {
        logger.debug("Accessing index page");
        try {
            SchedulerStatistics stats = scheduler.getStatistics();
            model.addAttribute("statistics", stats);
            model.addAttribute("isRunning", scheduler.isRunning());
            logger.debug("Index page data loaded successfully");
        } catch (Exception e) {
            logger.warn("Error loading scheduler data for index page: {}", e.getMessage());
            // 如果调度器未启动，使用默认值
            model.addAttribute("statistics", new SchedulerStatistics());
            model.addAttribute("isRunning", false);
        }
        return "index";
    }
    
    /**
     * 任务管理页面
     */
    @GetMapping("/tasks")
    public String tasks(Model model) {
        logger.debug("Accessing tasks page");
        try {
            model.addAttribute("totalTasks", scheduler.getAllTasks().size());
            model.addAttribute("pendingTasks", scheduler.getPendingTasks().size());
            model.addAttribute("runningTasks", scheduler.getTasksByStatus(TaskStatus.RUNNING.name()).size());
            model.addAttribute("successTasks", scheduler.getTasksByStatus(TaskStatus.SUCCESS.name()).size());
            model.addAttribute("failedTasks", scheduler.getTasksByStatus(TaskStatus.FAILED.name()).size());
            logger.debug("Tasks page data loaded successfully");
        } catch (Exception e) {
            logger.warn("Error loading tasks data: {}", e.getMessage());
            model.addAttribute("totalTasks", 0);
            model.addAttribute("pendingTasks", 0);
            model.addAttribute("runningTasks", 0);
            model.addAttribute("successTasks", 0);
            model.addAttribute("failedTasks", 0);
        }
        return "tasks";
    }
    
    /**
     * 监控统计页面
     */
    @GetMapping("/monitor")
    public String monitor(Model model) {
        logger.debug("Accessing monitor page");
        try {
            SchedulerStatistics stats = scheduler.getStatistics();
            model.addAttribute("statistics", stats);
            logger.debug("Monitor page data loaded successfully");
        } catch (Exception e) {
            logger.warn("Error loading monitor data: {}", e.getMessage());
            model.addAttribute("statistics", new SchedulerStatistics());
        }
        return "monitor";
    }
    
    /**
     * 系统设置页面
     */
    @GetMapping("/settings")
    public String settings(Model model) {
        logger.debug("Accessing settings page");
        try {
            model.addAttribute("isRunning", scheduler.isRunning());
            logger.debug("Settings page data loaded successfully");
        } catch (Exception e) {
            logger.warn("Error loading settings data: {}", e.getMessage());
            model.addAttribute("isRunning", false);
        }
        return "settings";
    }

    /**
     * 测试页面
     */
    @GetMapping("/test")
    public String test() {
        logger.debug("Accessing test page");
        return "test";
    }

}
